package com.atydh.gulimall.produck.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atydh.common.utils.PageUtils;
import com.atydh.common.utils.Query;

import com.atydh.gulimall.produck.dao.AttrGroupDao;
import com.atydh.gulimall.produck.entity.AttrGroupEntity;
import com.atydh.gulimall.produck.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        QueryWrapper<AttrGroupEntity> attrGroupEntity = new QueryWrapper<AttrGroupEntity>();
        if (catelogId==0){

            if (!StringUtils.isEmpty(params.get("key"))){
                String key = (String) params.get("key");
                attrGroupEntity.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    attrGroupEntity
            );

            return new PageUtils(page);

        }else {
            attrGroupEntity.eq("catelog_id", catelogId);
            if (!StringUtils.isEmpty(params.get("key"))){
                String key = (String) params.get("key");
                attrGroupEntity.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    attrGroupEntity
            );
            return new PageUtils(page);

        }

    }

}